<?php
/**
 * @title 393. UTF-8 编码验证 - 迭代
 * @author start2004
 */

// ini_set("memory_limit", "128M");

class Solution {

    /**
     * @param Integer[] $data
     * @return Boolean
     */
    function validUtf8($data) {
        /**
         * @since 2020-08-12 最低值
         */
        $mins = [
            5 => bindec("11111000"),
            4 => bindec("11110000"),
            3 => bindec("11100000"),
            2 => bindec("11000000"),
            1 => bindec("10000000"),
        ];

        /**
         * @since 2020-08-12 结果集
         */
        $result = true;

        /**
         * @since 2020-08-12 迭代查找
         */
        for($i=0, $dataLen=count($data); $i<$dataLen;){
            $num = $data[$i];

            /**
             * @since 2020-08-12 超出范围
             */
            if($num>=$mins[5]){
                $result = false;
                break;
            } elseif($num>=$mins[4]) {
                /**
                 * @since 2020-08-12 4个数, 1111 0xxx
                 */
                $len = 4;
            } elseif($num>=$mins[3]) {
                /**
                 * @since 2020-08-12 3个数, 1110 xxxx
                 */
                $len = 3;
            } elseif($num>=$mins[2]) {
                /**
                 * @since 2020-08-12 2个数, 1100 xxxx
                 */
                $len = 2;
            } elseif($num<$mins[1]) {
                /**
                 * @since 2020-08-12 1个数, 0xxx xxxx
                 */
                $len = 1;
            } else {
                /**
                 * @since 2020-08-12 超出范围
                 */
                $result = false;
                break;
            }

            /**
             * @since 2020-08-12 判断后续字符是否符合
             */
            for($j=1, $k=$i+1; $j<$len; $j++, $k++){
                if(isset($data[$k])){
                    /**
                     * @since 2020-08-12 小于 10xx xxxx
                     */
                    if($data[$k] < $mins[1]){
                        $result = false;
                        $i = $dataLen;
                        break;
                    } else {}
                } else {
                    /**
                     * @since 2020-08-12 后续数量不符合
                     */
                    $result = false;
                    $i = $dataLen;
                    break;
                }
            }

            /**
             * @since 2020-08-12 后续循环
             */
            $i += $len;
        }

        /**
         * @return
         */
        return $result;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202008/2020.08.12_3.php
 */
$datas = [
    // [197,130,1],
    // [197,130,1, 197],
    // [235,140,4],
    [248,130,130,130],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    var_dump($data);

    $obj = new Solution();
    $result = $obj->validUtf8($data);
    var_dump($result);
    echo str_repeat("<br>", 3);
}
$xhprof->end();
